<template>
  <div class="message-read">
    <div class="class-menu" v-if="isClassReceiver">
      <div
        class="menu-item"
        v-for="item in classList"
        :key="item.class_id"
        :class="{ active: curClass.class_id === item.class_id }"
        @click="curClass = item"
      >
        {{ item.class_name }}
      </div>
    </div>
    <div class="main-content">
      <div
        class="student-list"
        :class="{ isStudentReceiver: !isClassReceiver }"
      >
        <div
          class="student-item"
          :class="{ read: item.is_read }"
          v-for="item in readList"
          :key="item.target_id"
        >
          <div class="name">{{ item.target_name }}</div>
          <div class="classroom" v-if="!isClassReceiver">
            {{ item.grade_name }}{{ item.class_name }}
          </div>
        </div>
      </div>
      <div class="tip">
        <span class="text">已查阅人数:&nbsp;{{ readCount }}/{{ total }}</span>
        <span class="text red">红色表示未读</span>
        <span class="text blue">蓝色表示已查阅</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import _ from 'lodash'
import { reqGetMsgReadStatus } from '@/apis/message'
import { MsgReceiverEnum } from '../constant'

const props = defineProps({
  msgInfo: { type: Object }
})

const isClassReceiver = computed(
  () => props.msgInfo.target_type === MsgReceiverEnum.CLASSROOM
)
const classList = computed(() =>
  isClassReceiver.value ? props.msgInfo.class_info : []
)
const curClass = ref(classList.value[0])

const readCount = ref(0)
const total = ref(0)
const readList = ref([])
const getMsgReadStatus = async () => {
  const params = {
    message_id: props.msgInfo.message_id
  }
  // 发布对象为班级需要传 class_id
  if (isClassReceiver.value) {
    params.class_id = curClass.value.class_id
  }
  const res = await reqGetMsgReadStatus(params)
  const list = res.list || []
  readList.value = list
  readCount.value = res.read_count
  total.value = res.total
}

onMounted(() => {
  getMsgReadStatus()
})
</script>

<style lang="less" scoped>
.message-read {
  box-sizing: border-box;
  padding: 26px 0;
  width: 100%;
  height: calc(100% - 68px);
  display: flex;
  font-size: 18px;
  border-radius: 8px;
  background-color: #fff;

  .class-menu {
    position: relative;
    width: 142px;
    height: 100%;
    overflow: auto;
    &::after {
      content: '';
      position: absolute;
      top: 0;
      right: 0;
      display: inline-block;
      width: 1px;
      height: 100%;
      background-color: rgba(4, 11, 26, 0.15);
    }
    .menu-item {
      margin-bottom: 50px;
      text-align: center;
      color: #434853;
      cursor: pointer;
      &.active {
        color: @color-brand-regular;
      }
    }
  }

  .main-content {
    flex: 1;
    padding: 0 30px;
    overflow: auto;

    .student-list {
      display: grid;
      grid-template-columns: repeat(11, 1fr);
      gap: 30px;
      .student-item {
        width: 96px;
        height: 42px;
        line-height: 42px;
        text-align: center;
        color: #ff6d65;
        border-radius: 4px;
        background-color: rgba(255, 109, 101, 0.1);
        &.read {
          color: @color-brand-regular;
          background-color: rgba(66, 143, 255, 0.2);
        }
        .name,
        .classroom {
          width: 100%;
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
        }
      }
      &.isStudentReceiver {
        grid-template-columns: repeat(10, 1fr);
        .student-item {
          width: 130px;
          height: 76px;
          line-height: 30px;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
        }
      }
    }

    .tip {
      margin-top: 30px;
      font-size: 16px;
      color: #b4b6ba;
      .text {
        &:first-child {
          margin-right: 60px;
        }
        &:last-child {
          margin-left: 38px;
        }
        &.red,
        &.blue {
          position: relative;
          &::before {
            content: '';
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            left: -18px;
            width: 12px;
            height: 12px;
            display: inline-block;
            border-radius: 50%;
            background-color: #ff6d65;
          }
        }
        &.blue {
          &::before {
            background-color: @color-brand-regular;
          }
        }
      }
    }
  }
}
</style>
